package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyJuYingJinFu;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  安心分期&聚赢金服流量测试(北京)
 *
 *  @author WJB
 */
@ApiPlan(productId = "366", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5JuYingJinFuBJ implements ApiOrgPhoneMd5ConvertHandler {
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        apiDataExchangeParameter.setExtendUrl("?ldy=bjjy-zz-fzjq");
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("mobile", apiDataExchangeParameter.getPhoneMd5());
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 200);
        if (!code) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        apiApplyParameter.setExtendUrl("?ldy=bjjy-zz-fzjq");
        OrgApplyJuYingJinFu data = new OrgApplyJuYingJinFu();
        //  贷款额度
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            String money = ChooseEq.create()
                    .when(apiApplyParameter.getLoanAmount(), "1", "30000")
                    .when(apiApplyParameter.getLoanAmount(), "2", "50000")
                    .when(apiApplyParameter.getLoanAmount(), "3", "100000")
                    .when(apiApplyParameter.getLoanAmount(), "4", "200000")
                    .end();
            data.setQuota(money);
        }
        data.setUsername(apiApplyParameter.getUserName());
        data.setMobile(apiApplyParameter.getPhone());
        data.setCity(apiApplyParameter.getCity());
        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        data.setRemark(apiFieldConverter.getExtendInfo());
        String sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", "1")
                .when(apiApplyParameter.getSex(), "1", "2")
                .end("1");
        data.setSex(sex);
        //  年龄
        data.setAge(apiApplyParameter.getAge());

        //  房
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", "无房产")
                    .when(apiApplyParameter.getRealEstateInfo(), "2", "有房不抵押")
                    .when(apiApplyParameter.getRealEstateInfo(), "3", "有房可抵押")
                    .when(apiApplyParameter.getRealEstateInfo(), "4", "有房产")
                    .when(apiApplyParameter.getRealEstateInfo(), "5", "有按揭房产")
                    .when(apiApplyParameter.getRealEstateInfo(), "6", "有全款房产")
                    .end();
            data.setHouse(text);
        }
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "无车产")
                    .when(apiApplyParameter.getCarInfo(), "2", "有车不抵押")
                    .when(apiApplyParameter.getCarInfo(), "3", "有车可抵押")
                    .when(apiApplyParameter.getCarInfo(), "4", "有车")
                    .end();
            data.setCar(text);
        }
        if (ValidParam.isNotEmpty(apiApplyParameter.getOverdue())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getOverdue(), "1", "无逾期")
                    .when(apiApplyParameter.getOverdue(), "1", "当前有逾期")
                    .when(apiApplyParameter.getOverdue(), "1", "一年以内无逾期")
                    .end();
            data.setCredit(text);
        }
        //  公积金
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", "false")
                    .end("true");
            data.setIsGjj(text);
        }
        return JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
